﻿Imports System.Windows.Forms

Partial Public Class Settings

#Region "Constructors"
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        txtAbortTime.Text = My.Settings.AbortTime.ToString()
        txtKeyCheck.Text = My.Settings.KeyCheck.ToString()
        txtPanicBatch.Text = My.Settings.PanicBatchFile

    End Sub
#End Region

#Region "UI Handlers"

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub txtPanicBatch_GotFocus(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles txtPanicBatch.GotFocus
        Using dlg As New OpenFileDialog()
            dlg.Filter = "Command files (*.cmd)|*.cmd|Batch files (*.bat)|*.bat|All files (*.*)|*.*"
            dlg.Multiselect = False
            If dlg.ShowDialog() = Forms.DialogResult.OK Then
                txtPanicBatch.Text = dlg.FileName
            End If
        End Using
    End Sub

    Private Sub btnApply_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnApply.Click
        Dim n As Integer
        If Not Integer.TryParse(txtAbortTime.Text, n) Then
            MessageBox.Show("Abort time is a number of seconds.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        My.Settings.AbortTime = n
        If Not Integer.TryParse(txtKeyCheck.Text, n) Then
            MessageBox.Show("Key check time is a number of seconds.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        My.Settings.KeyCheck = n
        If String.Compare(My.Settings.Password, txtOldPassword.Password, False) = 0 And String.Compare(txtConfirmPassword.Password, txtNewPassword.Password, False) = 0 Then
            My.Settings.Password = txtNewPassword.Password
        End If
        If (String.Compare(txtOldPIN.Password, My.Settings.PinCode, False) = 0) And String.Compare(txtNewPIN.Password, txtConfirmPIN.Password, False) Then
            My.Settings.PinCode = txtNewPIN.Password
        End If
        My.Settings.PanicBatchFile = txtPanicBatch.Text
        My.Settings.Save()
        Me.Close()
    End Sub

#End Region

End Class
